package edu.cicese.alz.swing;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 11/07/12
 * Time: 03:04 PM
 */
public class ToggleButtonMouseListener extends MouseAdapter {

	private CustomToggleButton button;

	public ToggleButtonMouseListener(CustomToggleButton button) {
		this.button = button;
	}

	public void mouseEntered(MouseEvent evt) {
		if (button.isEnabled() && !button.isSelected()) {
			button.paintRollover();
		}
	}

	public void mouseExited(MouseEvent evt) {
		if (!button.isSelected()) {
			button.paintDeselected();
		}
	}

	public void mousePressed(MouseEvent evt) {
		if (button.isEnabled()) {
			button.setPressed(true);
		}
	}

	public void mouseReleased(MouseEvent evt) {
		if (button.isEnabled() && button.isPressed() && insideButton(evt.getX(), evt.getY())) {
			button.setPressed(false);
			button.setSelected(!button.isSelected());
			if (!button.isSelected()) {
				button.paintRollover();
			}
		}
	}

	public void mouseClicked(MouseEvent evt) {
	}

	private boolean insideButton(int x, int y) {
		return (x >= 0 && x < button.getWidth()) && (y >= 0 && y < button.getHeight());
	}
}
